package com.alphaking.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alphaking.Jdbc.JdbcUser;
import com.alphaking.Jdbc.JdbcUtils;
import com.alphaking.bean.User;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * 登陆校验
 * 
 * @author cxywan
 *
 */
public class LoginServlet extends HttpServlet {

	// 登陆时的账号，密码
	private String account;
	private String password;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		doPost(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 获得账号密码
		account = req.getParameter("account");
		password = req.getParameter("password");

		// 进行数据库连接
		if (JdbcUtils.getCon() == null) {

			JdbcUtils.getConnection();
		}
		// 检验
		check_login(req, resp);

	}

	/**
	 * 验证是否注册成功，并且跳转页面
	 * 
	 * @param req
	 * @param resp
	 * @param account
	 * @param password
	 * @throws IOException
	 */
	public void check_login(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		PrintWriter pw = resp.getWriter();
		int type = JdbcUser.check_login(account, password);
		if (type == 3) {// 验证成功
		pw.write("true");
		} else if (type == 2) {// 用户存在，但是密码错误
			pw.write("password");
		} else {// 用户不存在
			pw.write("account");
		}
	}

}
